package cn.xorange.commons.system.controller.platform;

import cn.xorange.commons.core.basic.bean.ResultBean;

import cn.xorange.commons.core.basic.bean.SystemCode;
import cn.xorange.commons.core.basic.controller.DataController;
import cn.xorange.commons.security.annotation.Authorize;
import cn.xorange.commons.security.annotation.PassToken;
import cn.xorange.commons.system.entity.platform.SysTenant;
import cn.xorange.commons.system.entity.platform.SysTenantProduct;
import cn.xorange.commons.system.entity.platform.SysTenantSpu;
import cn.xorange.commons.system.entity.platform.SysTenantUsers;
import cn.xorange.commons.system.service.platform.ISysTenantService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


/**
 * @author : yangjian
 * @date : 2021/12/28
 * @description :
 */
@RestController
@Authorize(hasPerm = "platform:tenant")
@RequestMapping("/platform/tenant")
public class SysTenantController extends DataController<SysTenant, ISysTenantService> {

    @PassToken
    @GetMapping({"/spu/{tenantId}", "/spu/{tenantId}/{spuStatus}"})
    public ResultBean getTenantSpu(@PathVariable(value = "tenantId")Long tenantId,
                                   @PathVariable(value = "spuStatus", required = false)String spuStatus){
        return ResultBean.onSuccess(this.service.getTenantSpu(tenantId, spuStatus));
    }

    @Authorize(hasPerm = "edit")
    @PutMapping("/spu")
    public ResultBean openTenantSpu(@Validated @RequestBody SysTenantSpu spu){
        if(Objects.isNull(spu.getSpuId())||Objects.isNull(spu.getTenantId())){
            return ResultBean.onFail(SystemCode.SYSTEM_PARAM_EXCEPTION);
        }
        this.service.openTenantSpu(spu.getTenantId(), spu.getSpuId());
        return ResultBean.onSuccess();
    }

    @Authorize(hasPerm = "edit")
    @DeleteMapping("/spu")
    public ResultBean closeTenantSpu(@RequestBody SysTenantSpu spu){
        if(Objects.isNull(spu.getSpuId())||Objects.isNull(spu.getTenantId())){
            return ResultBean.onFail(SystemCode.SYSTEM_PARAM_EXCEPTION);
        }
        this.service.closeTenantSpu(spu.getTenantId(), spu.getSpuId());
        return ResultBean.onSuccess();
    }

    @Authorize(hasPerm = "list")
    @GetMapping("/users/list")
    public ResultBean usersList(SysTenantUsers tenantUsers){
        return ResultBean.onSuccess(service.getUsersList(tenantUsers.getTenantId()));
    }

    @Authorize(hasPerm = "edit")
    @PostMapping("/users")
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean usersAdd(@RequestBody SysTenantUsers tenantUsers){

        if(Objects.isNull(tenantUsers.getTenantId()) || Objects.isNull(tenantUsers.getProductId())
                || Objects.isNull(tenantUsers.getCycleId()) || Objects.isNull(tenantUsers.getUsers())){
            return ResultBean.onFail(SystemCode.SYSTEM_PARAM_EXCEPTION);
        }
        service.setUsers(tenantUsers);
        return ResultBean.onSuccess();
    }

    @Authorize(hasPerm = "list")
    @GetMapping("/product/list")
    public ResultBean productList(SysTenantProduct tenantProduct){
        return ResultBean.onSuccess(service.getProductList(tenantProduct.getTenantId()));
    }

    @Authorize(hasPerm = "edit")
    @PostMapping("/product")
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean productAdd(@RequestBody SysTenantProduct tenantProduct){

        if(Objects.isNull(tenantProduct.getTenantId()) || Objects.isNull(tenantProduct.getProductId())
                || Objects.isNull(tenantProduct.getCycleId() )){
            return ResultBean.onFail(SystemCode.SYSTEM_PARAM_EXCEPTION);
        }
        service.setProduct(tenantProduct);
        return ResultBean.onSuccess();
    }


    /**
     * 通过商户Id 查询商户开通的模块 以及模块对应的产品
     * @param tenantId 商户Id
     */
    //@GetMapping("/modules/{tenantId}")
    //public ResultBean getModulesByTenantId(@PathVariable(value = "tenantId")Long tenantId){
    //    return ResultBean.onSuccess(this.service.getModulesByTenantId(tenantId));
    //}
    //
    ///**
    // * 平台管理侧 商户模块开通申请列表
    // * @param tenantApply
    // */
    //@Authorize(hasPerm = "apply")
    //@GetMapping("/apply/list")
    //public PageBean tenantApplyList(SysTenantApply tenantApply){
    //    super.startPage();
    //    List<SysTenantApply> list = this.service.tenantApplyList(tenantApply);
    //    return new PageBean(list);
    //}


    /**
     * 用户侧 商户申请模块开通
     * @param tenantApply
     * @return ResultBean
     */
    //@PostMapping("/apply")
    //public ResultBean addTenantApply(@Validated @RequestBody SysTenantApply tenantApply){
    //    SysTenant tenant = service.getById(tenantApply.getTenantId());
    //    // 通过module和商户类型 获取协议列表 根据传入的协议ID与列表匹配进行一次模块申请安全校验
    //    SysAgreement sysAgreement = new SysAgreement();
    //    sysAgreement.setModule(tenantApply.getModule());
    //    String type = tenant.getTenantType();
    //    if(type.equals("0")){
    //        // 系统商户等于企业商户
    //        type = "3";
    //    }
    //    sysAgreement.setType(type);
    //    List<SysAgreement> lst = this.agreementService.getList(sysAgreement);
    //    boolean check= false;
    //    for (SysAgreement agreement : lst){
    //        if (agreement.getAgreementId() == tenantApply.getAgreementId()){
    //            check = true;
    //            break;
    //        }
    //    }
    //    if (check){
    //        tenantApply.setTenantType(tenant.getTenantType());
    //        tenantApply.setResult("0");
    //        service.addTenantApply(tenantApply);
    //        return ResultBean.onSuccess();
    //    }else {
    //        return ResultBean.onFail("模块申请，校验失败");
    //    }
    //}

    /**
     * 平台管理侧 审批模块开通的申请
     */
    //@Authorize(hasPerm = "examine")
    //@PostMapping("/examine")
    //public ResultBean examineTenantApply(@Validated @RequestBody SysTenantApply tenantApply){
    //    if(Objects.isNull(tenantApply.getTenantId()) || SysConstants.UNDEFINED.equals(tenantApply.getTenantId()) ||
    //        Objects.isNull(tenantApply.getApplyId())) {
    //        return ResultBean.onFail("参数错误");
    //    }
    //    service.examineTenantApply(tenantApply);
    //    return ResultBean.onSuccess();
    //}



}
